﻿using System;
using System.Linq;
using System.Net;
using System.Web.Mvc;
using Matrix.MaKaYunShang.BLL;
using Matrix.MaKaYunShang.Model;

namespace Matrix.MaKaYunShang.MagWeb.Controllers
{
    [Authorization]
    [Matrix.MaKaYunShang.Model.Attributes.Skip]
    public class CardCategoryController : BaseController
    {
        #region 列表
        public ActionResult List()
        {
            return View();
        }

        [HttpPost]
        [Matrix.MaKaYunShang.Model.Attributes.Skip]

        public JsonResult Listing(int draw, int start, int length)
        {
            try
            {
                int order;
                int.TryParse(Request["order[0][column]"], out order);
                var direction = Request["order[0][dir]"];
                var orderByColumns = new[] { "CategoryName", "CategoryCode", "Description", "WarningInfo" };
                var search = Request["search[value]"];
                var list = CardCategoryBLL.Instance.GetPagingList(draw, (start / length) + 1, length, SqlSafe(search), orderByColumns[order] + " " + direction);
                list.data.ForEach(a =>
                {
                    if (MagModules.FindAll(t => t
                        .Action.Equals("Update") && t
                            .Controller.Equals("CardCategory")).Count > 0)
                    {
                        a.Action = "<a class='btn btn-warning' href='javascript:;' onclick=updateData(" + a.Id +
                                   ");>修改</a> ";
                    }
                    if (MagModules.FindAll(t => t
                     .Action=="Update" && t
                         .Controller=="CardCategory").Count>0)
                    {
                        a.Action += "<a class='btn btn-danger' href='javascript:;' onclick=deleteData(" + a.Id + ");>删除</a>";
                    }

                });
                return Json(list);
            }
            catch (Exception ex)
            {
                Logger.Error("CardCategory Listing Catch Error: " + ex.Message);
                return Json(false);
            }
        }
        #endregion

        #region 创建
        public ActionResult Create()
        {
            return View();
        }

        [HttpPost]
        public JsonResult Create(CardCategory model)
        {
            var result = new Result();
            var checkModel = CardCategoryBLL.Instance.GetModelList("CategoryCode='" + SqlSafe(model.CategoryCode) + "'").FirstOrDefault();
            if (checkModel != null)
            {
                result.Message = "分类编码已存在，请更换";
                return Json(result);
            }
            result.Status = CardCategoryBLL.Instance.Add(model) > 0;
            if (result.Status == false)
            {
                result.Message = "添加失败，请重试";
            }
            return Json(result);
        }
        #endregion

        #region 修改
        public ActionResult Update(int id)
        {
            if (id <= 0)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            var model = CardCategoryBLL.Instance.GetModel(id);
            if (model == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.NotFound);
            }
            return View(model);
        }

        [HttpPost]
        public JsonResult Update(CardCategory model)
        {
            var result = new Result();
            var temp = CardCategoryBLL.Instance.GetModel(model.Id);
            if (temp == null)
            {
                result.Message = "保存失败，请重试";
                return Json(result);
            }
            temp.CategoryName = model.CategoryName;
            temp.Description = model.Description;
            temp.WarningInfo = model.WarningInfo;
            result.Status = CardCategoryBLL.Instance.Update(temp);
            if (result.Status == false)
            {
                result.Message = "保存失败，请重试";
            }
            return Json(result);
        }
        #endregion

        #region 删除
        [HttpPost]
        public JsonResult Delete()
        {
            var result = CardCategoryBLL.Instance.DeleteList(Request["doIds"]);
            return Json(result);
        }
        #endregion
    }
}
